<?php

namespace app\controllers;

use Yii;
use app\models\Recall;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\commands\Service;
use app\models\Patient;

/**
 * RecallController implements the CRUD actions for Recall model.
 */
class RecallController extends Service
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Recall models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Recall::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Recall model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Recall model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Recall();
        if(Yii::$app->request->isAjax){
            $res = ['status'=>'success', 'msg'=>''];
            $var = Yii::$app->request->post();
            $patient = Patient::findOne($var['patient_id']);
            $model->user_id = Yii::$app->user->getId();
            $model->patient_id = $var['patient_id'];
            $model->content = $var['content'];
            $patient->last_recall_time = $model->createtime = date('Y-m-d H:i:s');
            if($var['order_time'] != null && date(trim($var['order_time'])) > date('2010-01-01') && empty($patient->arrive_time)){
                $model->lastordertime = $patient->order_time;
                $model->ordertime = date(trim($var['order_time']));
                $patient->order_time = date(trim($var['order_time']));
            }

            if($patient->status == '未回访'){
                $patient->status = '已回访';
            }
            $model->kinds = $var['review_type'];
            if(in_array($var['review_type'], ['已改约', '流失'])){
                $patient->status = $var['review_type'];
            }
            
            if($model->save()){
                if($patient->save()){
                    $res['msg'] .= '患者状态已更新，';
                }
                $res['msg'] .= '患者:'. $patient->name .' 的回访记录添加成功';
                return json_encode($res);
            }
        }

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing Recall model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing Recall model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Recall model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Recall the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Recall::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
